﻿
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MyWeb.Common;
using MyWeb.Business;
using MyWeb.Data;
namespace MyWeb.Admins
{
    public partial class tblImages : System.Web.UI.Page
    {
        static bool Insert = false;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Session["folder"] = "tblImages"; BindGrid();
            }
        }
        private void BindGrid()
        {
            grdtblImages.DataSource = tblImagesService.tblImages_GetByAll();
            grdtblImages.DataBind();
        }
        protected void btnThem_Click(object sender, EventArgs e)
        {
            pnShow.Visible = false;
            pnUpdate.Visible = true;
            ControlClass.ResetControlValues(this);
            Session["upload"] = null;
            Insert = true;
        }
        protected void btnXoa_Click(object sender, EventArgs e)
        {
            try
            {
                DataGridItem item = default(DataGridItem);
                for (int i = 0; i < grdtblImages.Items.Count; i++)
                {
                    item = grdtblImages.Items[i];
                    if (item.ItemType == ListItemType.AlternatingItem | item.ItemType == ListItemType.Item)
                    {
                        if (((CheckBox)item.FindControl("ChkSelect")).Checked)
                        {
                            string strId = item.Cells[1].Text;
                            try
                            {
                                tblImagesService.tblImages_Delete(strId);
                            }
                            catch
                            { }
                        }
                    }
                } grdtblImages.CurrentPageIndex = 0; BindGrid();
            }
            catch { }
        }
        protected void grdtblImages_ItemCommand(object source, DataGridCommandEventArgs e)
        {
            Data.tblImagesInfo strCA = new tblImagesInfo();
            strCA._imgid = e.CommandArgument.ToString();
            if (e.CommandName == "Edit")
            {
                DataTable dt = tblImagesService.tblImages_GetById(strCA);
                dll_iaid_Update.SelectedValue = dt.Rows[0]["_iaid"].ToString();
                txt_imgid.Text = dt.Rows[0]["_imgid"].ToString();
                txt_imgname.Text = dt.Rows[0]["_imgname"].ToString();
                txt_imglink.Text = dt.Rows[0]["_imglink"].ToString();
                txt_imgcreatedate.Text = dt.Rows[0]["_imgcreatedate"].ToString();
                pnUpdate.Visible = true;
                pnShow.Visible = false;
            }
            else
            {
                tblImagesService.tblImages_Delete(strCA._iaid); BindGrid();
            }
        }
        protected void lblBack_Click(object sender, EventArgs e)
        {
            pnUpdate.Visible = false; pnShow.Visible = true; Insert = false;
        }
        protected void lblUpdate_Click(object sender, EventArgs e)
        {
            string image = Session["upload"] == null ? "" : Session["upload"].ToString();
            #region[TestInput]
            if (dll_iaid_Update.SelectedValue.Trim().Equals(""))
            {
                WebMsgBox.Show("_iaid không thể trống !");
                dll_iaid_Update.Focus();
                return;
            }
            if (txt_imgid.Text.Trim().Equals(""))
            {
                WebMsgBox.Show("_imgid không thể trống !");
                txt_imgid.Focus();
                return;
            }
            if (txt_imgname.Text.Trim().Equals(""))
            {
                WebMsgBox.Show("_imgname không thể trống !");
                txt_imgname.Focus();
                return;
            }
            if (txt_imglink.Text.Trim().Equals(""))
            {
                WebMsgBox.Show("_imglink không thể trống !");
                txt_imglink.Focus();
                return;
            }
            if (txt_imgcreatedate.Text.Trim().Equals(""))
            {
                WebMsgBox.Show("_imgcreatedate không thể trống !");
                txt_imgcreatedate.Focus();
                return;
            }
            #endregion
            Data.tblImagesInfo obj = new Data.tblImagesInfo();
            obj._iaid = dll_iaid_Update.SelectedValue;
            obj._imgid = txt_imgid.Text; obj._imgname = txt_imgname.Text;
            obj._imglink = txt_imglink.Text;
            obj._imgcreatedate = txt_imgcreatedate.Text; if (Insert == true)
            {
                tblImagesService.tblImages_Insert(obj);
            }
            else
            {
                tblImagesService.tblImages_Update(obj);
            }
            BindGrid();
            pnShow.Visible = true;
            pnUpdate.Visible = false;
            Insert = false;
        }
        protected void grdtblImages_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
        {
            grdtblImages.CurrentPageIndex = e.NewPageIndex; BindGrid();
        }
        private void ShowDll()
        {
            var dt = new DataTable();
            dt = tblImgAlbumService.tblImgAlbum_GetByAll();
            dll_iaid.DataSource = dt;
            dll_iaid.DataTextField = "";
            dll_iaid.DataValueField = "";
            dll_iaid.DataBind();
            dll_iaid_Update.DataSource = dt;
            dll_iaid_Update.DataTextField = "";
            dll_iaid_Update.DataValueField = "";
            dll_iaid_Update.DataBind();
        }
        protected void btnshow_Click(object sender, EventArgs e)
        {
            grdtblImages.DataSource = tblImagesService.tblImages_GetBy_iaid(dll_iaid.SelectedValue);
            grdtblImages.DataBind();
        }
        protected void grdtblImages_ItemDataBound(object sender, DataGridItemEventArgs e)
        {
            ListItemType itemType = e.Item.ItemType;
            if ((itemType != ListItemType.Footer) && (itemType != ListItemType.Separator))
            {
                if (itemType == ListItemType.Header)
                {
                    object checkBox = e.Item.FindControl("chkSelectAll");
                    if ((checkBox != null))
                    {
                        ((CheckBox)checkBox).Attributes.Add("onClick", "Javascript:chkSelectAll_OnClick(this)");
                    }
                }
                else
                {
                    string tableRowId = grdtblImages.ClientID + "_row" + e.Item.ItemIndex.ToString();
                    e.Item.Attributes.Add("id", tableRowId);
                    object checkBox = e.Item.FindControl("chkSelect");
                    if ((checkBox != null))
                    {
                        e.Item.Attributes.Add("onMouseMove", "Javascript:chkSelect_OnMouseMove(this)");
                        e.Item.Attributes.Add("onMouseOut", "Javascript:chkSelect_OnMouseOut(this," + e.Item.ItemIndex + ")");
                        ((CheckBox)checkBox).Attributes.Add("onClick", "Javascript:chkSelect_OnClick(this," + e.Item.ItemIndex + ")");
                    }
                }
            }
        }
    }
}